package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsClient;
import com.yidu.ruifeng_logistics.entity.LogisticsSign;
import com.yidu.ruifeng_logistics.service.LogisticsSignService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.utils.Tools;
import com.yidu.ruifeng_logistics.vo.LogisticsSignVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsSign)表控制层
 *
 * @author lcd
 *
 * @since 2021-03-09 14:29:16
 */
@Api(description = "签收接口")
@RestController
@RequestMapping("logisticsSign")
public class LogisticsSignController {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    /**
     * 服务对象
     */
    @Resource
    private LogisticsSignService logisticsSignService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据签收id查询分类", notes = "返回唯一的签收")
    @GetMapping("selectOne")
    public LogisticsSign selectOne(Integer id) {
        return this.logisticsSignService.queryById(id);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param orderId 主键
     * @return 单条数据
     */
    @ApiOperation(value = "查询订单id数", notes = "返回唯一的签收")
    @GetMapping("queryByOrderId")
    public Map<String, Object> queryByOrderId(Integer orderId) {
        Map<String, Object> map = new HashMap<>();
        List<LogisticsSign> list = logisticsSignService.queryByOrderId(orderId);
        map.put("code", 1);
        map.put("data", list.size());
        return map;
    }

    /**
     * 查全
     *
     * @param vo
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查全", notes = "返回所有签收")
    @GetMapping("selectAll")
    public Map<String, Object> selectAll(LogisticsSignVo vo, Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        LogisticsSign logisticsSign = new LogisticsSign();
        System.out.println(vo.toString());
        //将vo改成LogisticsSign
        if (vo.getSignTime() != null && vo.getSignTime() != "") {
            System.out.println("=======================" + vo.getSignTime());
            Tools tool = new Tools();
            LocalDateTime ldt = tool.parseStringToLocalDateTime(vo.getSignTime(), "yyyy-MM-dd");

            logisticsSign.setSignTime(ldt);
        }
        //查询收件人
        logisticsSign.setSignRecipients(vo.getSignRecipients());
        //查询收件人电话
        logisticsSign.setSignTel(vo.getSignTel());
        //将page，limit传入分页助手
        PageHelper.startPage(page, limit);
        //查全
        List<LogisticsSign> list = logisticsSignService.queryAll(logisticsSign);
        PageInfo<LogisticsSign> pageInfo = new PageInfo<>(list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list); //这里可以直接写list
        map.put("count", pageInfo.getTotal());//总行数自动生成
        return map;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据签收id删除", notes = "")
    @DeleteMapping("delete")
    public BaseMessage delete(Integer id) {
        boolean i = logisticsSignService.deleteById(id);
        if (i) {
            return BaseMessage.success();
        } else {
            return BaseMessage.fail();
        }

    }

    /**
     * 增加和修改
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "增改")
    @PostMapping("insertAndUpdate")
    public BaseMessage insertAndUpdate(LogisticsSignVo vo) {
        BaseMessage baseMessage = new BaseMessage();
        //获得LocalDateTime类型的当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        LogisticsSign logisticsSign = new LogisticsSign();
        if (vo.getSignId() == null) {//vo有signid则增
            //考虑到signid为空不能copyProperties
            logisticsSign.setOrderId(vo.getOrderId());
            logisticsSign.setSignArea(vo.getSignArea());
            logisticsSign.setSignRecipients(vo.getSignRecipients());
            logisticsSign.setSignTel(vo.getSignTel());
            logisticsSign.setSignTime(localDateTime);
            logisticsSignService.insert(logisticsSign);
            baseMessage.setState(1);
            baseMessage.setMsg("增加成功");
        } else {//修改
            //copyProperties vo
            BeanUtils.copyProperties(vo, logisticsSign);
            logisticsSign.setSignTime(localDateTime);
            logisticsSignService.update(logisticsSign);
            baseMessage.setState(1);
            baseMessage.setMsg("修改成功");
        }
        return BaseMessage.success();
    }

    /**
     * 显示登陆客户所有的信息
     *
     * @param session
     * @return
     */
    @ApiOperation(value = "显示登陆客户所有的信息")
    @PostMapping("/orderInformation")
    public Map<String, Object> orderInformation(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //根据session identity键值判断客户和员工
        String s = (String) session.getAttribute("identity");
        if ("client".equals(s)) {
            LogisticsClient logisticsClient = (LogisticsClient) session.getAttribute("login");
            map.put("code", 1);
            map.put("msg", "");
            map.put("data", logisticsClient);
        } else {
            map.put("code", 0);
            map.put("msg", "你没有登录");
        }


        return map;
    }

}
